package com.e3.template.tgf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.web.core.client.GXTHelper;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.core.client.ListenerManager;
import com.e3.web.core.client.QueryListener;
import com.e3.web.core.model.SuperModel;
import com.e3.web.core.model.SuperQC;
import com.e3.web.widget.client.MyDialog;
import com.e3.web.widget.client.MyField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;

public abstract class TGFQueryDialog<D extends SuperModel> extends MyDialog {
	
	private TGFMessages TGFMESSAGES = TGFMessages.INSTANCE;
	
	private ListenerManager<D> listenerManager = new ListenerManager<D>();
	
	private FormPanel formPanel = new FormPanel();
	
	private List<Field<?>> fields;
	
	private FormData formData = new FormData("100%");
	
	private Map<String,Field<?>> formFieldsMap = new HashMap<String, Field<?>>();
	
	private String localName;
	
	private boolean isPagingQuery = false;
	
	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		this.setSize(400, 300);
		this.setLayout(new FitLayout());
		this.setHeading(TGFMESSAGES.title_query(getLocalName()));
		
		formPanel.setHeaderVisible(false);
		this.add(formPanel);
		
		initFormPanel();
		
	}
	
	private void initFormPanel() {
		if(getFormFields() == null){
			return;
		}
		fields = getFormFields();
		for(int i=0;i<fields.size();i++){
			Field<?> field = fields.get(i);
			formPanel.add(field, formData);
			formFieldsMap.put(field.getName(), field);
		}
	}

	public String getLocalName() {
		return localName;
	}

	public void setLocalName(String localName) {
		this.localName = localName;
	}

	public void addQueryListener(QueryListener<D> listener){
		listenerManager.addQueryListener(listener);
	}
	
	public <T extends SuperQC> void onQuerySuccess(T qc,List<D> result){
		unmask();
		listenerManager.notifyQuerySuccess(qc,result);
		setVisible(false);
	}
	
	protected MyField<?> getMyFormField(String name){
		return  (MyField<?>)formFieldsMap.get(name);
	}
	
	protected ListenerManager<D> getListenerManager(){
		return listenerManager;
	}
		
	public boolean isPagingQuery() {
		return isPagingQuery;
	}

	public void setPagingQuery(boolean isPagingQuery) {
		this.isPagingQuery = isPagingQuery;
	}
	
	protected <T extends SuperQC> GwtAsyncCallback<List<D>> getQueryAsyncCallBack(final T qc){
		return new GwtAsyncCallback<List<D>>(this) {
			@Override
			public void doSuccess(List<D> result) {
				onQuerySuccess(qc,result);
			}
		};
	}

	protected void onOk(){
		SuperQC qc = getQueryCondition();
		GXTHelper.maskQuery(this);
		executeQueryService(qc,getQueryAsyncCallBack(qc));
	}
	
	protected abstract SuperQC getQueryCondition();
	
	protected abstract List<Field<?>> getFormFields();
	
	protected abstract void executeQueryService(SuperQC qc,GwtAsyncCallback<List<D>> callback);
		
}
